package fu.capstone.hanoicityguide.presentation.fragment.food;

import android.app.Fragment;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.mapquest.android.maps.DefaultItemizedOverlay;
import com.mapquest.android.maps.GeoPoint;
import com.mapquest.android.maps.MapView;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.food.FoodBO;
import fu.capstone.hanoicityguide.business.bo.food.FoodBOImp;
import fu.capstone.hanoicityguide.business.model.Food;
import fu.capstone.hanoicityguide.common.onlinemapultility.OnlineMapUtil;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.LocationSetting;

public class FoodOnlineMapFragment extends Fragment {

	private OnlineMapUtil util;
	private Context context;
	private DefaultItemizedOverlay overlay;
	private MapView map;
	private WebView instruction;
	private RelativeLayout maplayout;
	private RelativeLayout webview;
	private InternetSetting internetSetting;
	private LocationSetting locationSetting;
	private FoodBO foodBO;
	private GeoPoint food_geo;
	private Menu menu = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View foodOnlineMapFragment = inflater.inflate(
				R.layout.online_map_fragment, container, false);

		setHasOptionsMenu(true);
		util = new OnlineMapUtil();
		context = container.getContext();
		Drawable icon = context.getResources().getDrawable(
				R.drawable.location_marker);
		overlay = new DefaultItemizedOverlay(icon);

		map = (MapView) foodOnlineMapFragment.findViewById(R.id.mapview);
		instruction = (WebView) foodOnlineMapFragment
				.findViewById(R.id.webview);
		maplayout = (RelativeLayout) foodOnlineMapFragment
				.findViewById(R.id.maplayout);
		webview = (RelativeLayout) foodOnlineMapFragment
				.findViewById(R.id.itineraryLayout);
		WebSettings settings = instruction.getSettings();
		settings.setDefaultTextEncodingName("utf-8");

		// getting GPS + internet status
		locationSetting = new LocationSetting(context);
		internetSetting = new InternetSetting(context);

		if (!locationSetting.getGPSSetting(container.getContext())) {
			locationSetting.showSettingsAlert();
		} else if (!internetSetting.getConnectivityStatus(container.getContext())) {
			internetSetting.showSettingsAlert();
		} else {
			util.setupmap(map);
			Bundle bundle = this.getArguments();
			int foodID = bundle.getInt("FoodID", 0);
			if (foodID != 0) {

				foodBO = new FoodBOImp(container.getContext());
				Food food = foodBO
						.browseFood(foodID);
				food_geo = util.convertGeocoordinatetoGeopoint(food
						.getGeo());
				
				util.createMarker(container.getContext(), map, food_geo,
						food.getName(), "");
				map.getController().setCenter(food_geo);
			}
		}
		
		
		return foodOnlineMapFragment;
	}
	
	
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.map_place_page_menu, menu);
		setMenu(menu);
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	public Menu getMenu() {
		return menu;
	}


	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_mylocation:
			util.getcurrentLocation(context, map);
			break;
		case R.id.action_routing:
			util.createRoute(context, map, instruction,
					String.valueOf(food_geo).substring(9));
			getMenu().getItem(2).setVisible(true);
			break;
		case R.id.action_instruction:
			maplayout.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
			getMenu().getItem(3).setVisible(true);
			getMenu().getItem(0).setVisible(false);
			getMenu().getItem(1).setVisible(false);
			getMenu().getItem(2).setVisible(false);
			break;
		case R.id.action_showmap:
			maplayout.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
			getMenu().getItem(3).setVisible(false);
			getMenu().getItem(0).setVisible(true);
			getMenu().getItem(1).setVisible(true);
			getMenu().getItem(2).setVisible(true);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.getItem(2).setVisible(false);
		menu.getItem(3).setVisible(false);
		super.onPrepareOptionsMenu(menu);
	}
}
